package com.seveneight.baijiesteward.ui.user.fragment.identify;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.BankCardParams;
import com.baidu.ocr.sdk.model.BankCardResult;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseFragmentBack;
import com.seveneight.baijiesteward.bean.BankBean;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.ui.user.contract.GetBankListContract;
import com.seveneight.baijiesteward.ui.user.fragment.IdentifyFragment;
import com.seveneight.baijiesteward.ui.user.presenter.GetBankListPresenter;
import com.seveneight.baijiesteward.utils.FileUtil;
import com.seveneight.baijiesteward.utils.GlideLoader;
import com.seveneight.baijiesteward.utils.LogUtil;
import com.seveneight.baijiesteward.utils.MDialog;
import com.seveneight.baijiesteward.utils.PathManagetUtil;
import com.seveneight.baijiesteward.utils.ToastUtil;
import com.seveneight.baijiesteward.views.BankListDialog;

import java.io.File;
import java.util.List;

/**
 * Created by user1 on 2018/2/23.
 */

public class IdentifyFragment2 extends BaseFragmentBack implements View.OnClickListener, GetBankListContract.GetBankView {


    GetBankListContract.GetBankPresenter getBankPresenter;
    private static final int REQUEST_CODE_CAMERA = 103;
    private ImageView iv_step2;
    private TextView tv_bank;
    private EditText tv_idNumber;
    private String filePath;
    private MDialog dialog;
    private ExpandableListView mElv;
    private BankListDialog bankListDialog;
    private EditText et_mobile;

    @Override
    protected int getContentView() {
        return R.layout.identify2;
    }

    @Override
    protected void initView() {
        setSwipeBackEnable(false);
        mElv = findView(R.id.elv);
        iv_step2 = findView(R.id.iv_step2);
        tv_bank = findView(R.id.tv_bank);
        et_mobile = findView(R.id.et_mobile);
        tv_idNumber = findView(R.id.tv_idNumber);
        dialog = new MDialog(mContext, "正在识别...");
    }

    @Override
    protected void initData() {
        getBankPresenter = new GetBankListPresenter(this);
        getBankPresenter.getBankList(mContext,jsonFormat.commonParam());
    }

    @Override
    protected void setListener() {
        tv_bank.setOnClickListener(this);
        iv_step2.setOnClickListener(this);
        tv_idNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                IdentifyFragment.BankCardNo = tv_idNumber.getText().toString().trim();
            }
        });
        et_mobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                IdentifyFragment.BankMobile = et_mobile.getText().toString().trim();
            }
        });
    }

    private void scanBankCard() {
        final String fileName = System.currentTimeMillis() + ".jpg";
        filePath= PathManagetUtil.getSavePath()+fileName;
        Intent intent = new Intent(mContext, CameraActivity.class);
        intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,filePath);
        intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                CameraActivity.CONTENT_TYPE_BANK_CARD);
        startActivityForResult(intent, REQUEST_CODE_CAMERA);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) {
            dialog.showLoadingDialog();
            if (data != null) {
                String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
                if (!TextUtils.isEmpty(contentType)) {
                    if (CameraActivity.CONTENT_TYPE_BANK_CARD.equals(contentType)) {
                        recBankCard(filePath);
                    }
                }
            }
        }
    }

    private void recBankCard(String filePath) {
        // 银行卡识别参数设置
        BankCardParams param = new BankCardParams();
        param.setImageFile(new File(filePath));

        // 调用银行卡识别服务
        OCR.getInstance().recognizeBankCard(param, new OnResultListener<BankCardResult>() {
            @Override
            public void onResult(BankCardResult result) {
                if (result != null) {
                    refreshUI(result);

                }
            }

            @Override
            public void onError(OCRError error) {
                showDialog("识别失败，请重新识别");
                Log.d("MainActivity", "onError: " + error.getMessage());
            }
        });
    }

    private void refreshUI(BankCardResult result) {
        IdentifyFragment.STEP = 4;
        IdentifyFragment.IMGSPATH2 = filePath;
        tv_bank.setText(!TextUtils.isEmpty(result.getBankName()) ? result.getBankName() : "");
        tv_idNumber.setText(!TextUtils.isEmpty(result.getBankCardNumber()) ? result.getBankCardNumber().replace(" ","") : "");
        GlideLoader.displayImg(mContext, filePath, iv_step2);
        dialog.hideDialog();
        for (int i = 0; i < data.size(); i++) {
            BankBean bean = data.get(i);
            if (bean.getBankName().contains(result.getBankName())) {
                IdentifyFragment.BankId = bean.getId();
                have=true;
                break;
            }
        }
        if(!have){
            showDialog("请手动选择银行种类或更换银行卡");
        }
    }

    private boolean have=false;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_step2:
                scanBankCard();
                break;
            case R.id.tv_bank:
                if (bankListDialog == null) {
                    bankListDialog = new BankListDialog(mContext, data, tv_bank,IdentifyFragment.BankId);
                }
                bankListDialog.show();
                break;
        }
    }

    private String id;
    private List<BankBean> data;

    @Override
    public void rebackBankList(List<BankBean> data) {
        LogUtil.d("banklist:" + data.toString());
        this.data = data;
    }
}
